package printParse.excel.jxlExcel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class SimpleExcelWrite {
	public void createExcel(OutputStream os) throws IOException,
			RowsExceededException, WriteException {

		// 创建工作簿
		WritableWorkbook workbook = Workbook.createWorkbook(os);
		// 创建新的一页
		WritableSheet sheet = workbook.createSheet("first sheet", 0);
		sheet.setName("fuqinqin");

		// 创建要显示的内容，创建一个单元格，第一个参数为列坐标，第二个参数为行坐标，第三个参数内容
		Label xuexiao = new Label(0, 0, "学校");
		sheet.addCell(xuexiao);
		Label zhuanye = new Label(1, 0, "专业");
		sheet.addCell(zhuanye);
		Label jingzhengli = new Label(2, 0, "专业竞争力");
		sheet.addCell(jingzhengli);

		Label qinghua = new Label(0, 1, "清华大学");
		sheet.addCell(qinghua);
		Label jisuanji = new Label(1, 1, "计算机专业");
		sheet.addCell(jisuanji);
		Label gao = new Label(2, 1, "高");
		sheet.addCell(gao);

		Label beida = new Label(0, 2, "北京大学");
		sheet.addCell(beida);
		Label falv = new Label(1, 2, "法律专业");
		sheet.addCell(falv);
		Label zhong = new Label(2, 2, "中");
		sheet.addCell(zhong);

		Label beihang = new Label(0, 3, "北京航空航天大学");
		sheet.addCell(beihang);
		Label guangdian = new Label(1, 3, "光电专业");
		sheet.addCell(guangdian);
		Label di = new Label(2, 3, "低");
		sheet.addCell(di);

		// 合并单元格 arg1起始列角标 arg2起始行角标 ar3终止列角标 arg4终止行角标
		sheet.mergeCells(0, 4, 2, 4);
		Label hebing = new Label(0, 4, "合并测试");
		WritableCellFormat wcf = new WritableCellFormat();
		wcf.setAlignment(Alignment.CENTRE);
		// 垂直居中
		wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
		// 是否换行
		wcf.setWrap(true);
		hebing.setCellFormat(wcf);
		sheet.addCell(hebing);

		// 设置冻结单元格，如下是设置第一行为冻结行，上下拉动滚动条始终显示
		sheet.getSettings().setVerticalFreeze(1);

		// 设置某行的行高度
		sheet.setRowView(4, 600);
		// 设置某列的宽度
		sheet.setColumnView(0, 20);

		// 将创建的北荣写入到输入流中，并且关闭输出流
		workbook.write();
		workbook.close();
		os.close();

	}

	public static void main(String[] args) throws RowsExceededException,
			WriteException, IOException {

		File file = new File("D://test.xls");
		OutputStream os = new FileOutputStream(file);
		SimpleExcelWrite sew = new SimpleExcelWrite();
		sew.createExcel(os);
		System.out.println("--SUCCESS--");

	}
}
